package com.surpassun.emall.service.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.surpassun.emall.exception.NoSuchProductException;
import com.surpassun.emall.model.Category;
import com.surpassun.emall.model.Country;
import com.surpassun.emall.model.Product;
import com.surpassun.emall.model.Store;
import com.surpassun.emall.repository.ProductRepository;
import com.surpassun.emall.service.ProductService;
import com.surpassun.emall.util.QueryUtil;
import com.surpassun.emall.util.Validator;

/**
 *
 * @author Denis
 */
@Service
public class ProductServiceImpl extends AbstractServiceImpl<Product> implements ProductService {

	@Inject
	private ProductRepository productRepository;
	
    public ProductServiceImpl() {
    	super(Product.class);
    }

	@Override
	public void addProduct(String name, String description,
			String longDescription, Store store, Date activeStartDate, Date activeEndDate, 
			Category category, float price, float promoPrice, int quantity, String height,
			String width, String length, String weight, String manufacturer, Country region, boolean active) {
		if (Validator.isNotNull(name)) {
			Product product = new Product(name, description, longDescription, store, new Date(),
					activeStartDate, activeEndDate, category, price, promoPrice, quantity, 
					height, width, length, weight, manufacturer, region, active);
			productRepository.add(product);
		}
	}

	@Override
	public void updateProduct(Product product, String name,
			String description, String longDescription, Store store, Date activeStartDate, Date activeEndDate,
			Category category, float price, float promoPrice, int quantity, String height,
			String width, String length, String weight, String manufacturer, Country region, boolean active) {
		if (Validator.isNotNull(product)) {
			product.setName(name);
			product.setDescription(description);
			product.setLongDescription(longDescription);
			product.setStore(store);
			product.setActiveStartDate(activeStartDate);
			product.setActiveEndDate(activeEndDate);
			product.setCategory(category);
			product.setPrice(price);
			product.setPromoPrice(promoPrice);
			product.setQuantity(quantity);
			product.setHeight(height);
			product.setWidth(width);
			product.setLength(length);
			product.setWeight(weight);
			product.setManufacturer(manufacturer);
			product.setRegion(region);
			product.setActive(active);
			productRepository.update(product);
		}
	}

	@Override
	public void updateProduct(long productId, String name,
			String description, String longDescription, Store store, Date activeStartDate, Date activeEndDate,
			Category category, float price, float promoPrice, int quantity, String height,
			String width, String length, String weight, String manufacturer,
			Country region, boolean active) throws NoSuchProductException {
		Product product = productRepository.get(productId);
		if (Validator.isNull(product)) {
			throw new NoSuchProductException("No such product exist with id : " + productId);
		}

		this.updateProduct(product, name, description, longDescription, store, 
				activeStartDate, activeEndDate, category, price, promoPrice, 
				quantity, height, width, length, weight, manufacturer, region, active);
	}

	@Override
	public void updateProduct(Product product, String name,
			String description, String longDescription) {
		if (Validator.isNotNull(product)) {
			product.setName(name);
			product.setDescription(description);
			product.setLongDescription(longDescription);
			productRepository.update(product);
		}
	}

	@Override
	public void activateProduct(Product product) {
		if (Validator.isNotNull(product)) {
			product.setActive(true);
			productRepository.update(product);
		}
	}

	@Override
	public void desactivateProduct(Product product) {
		if (Validator.isNotNull(product)) {
			product.setActive(false);
			productRepository.update(product);
		}
	}

	@Override
	public void changeProductPrice(Product product, float price) {
		if (Validator.isNotNull(product)) {
			product.setPrice(price);
			productRepository.update(product);
		}
	}

	@Override
	public void changeProductPromoPrice(Product product, float promoPrice) {
		if (Validator.isNotNull(product)) {
			product.setPromoPrice(promoPrice);
			productRepository.update(product);
		}
	}

	@Override
	public void changeProductQuantity(Product product, int quantity) {
		if (Validator.isNotNull(product)) {
			product.setQuantity(quantity);
			productRepository.update(product);
		}
	}

	@Override
	public List<Product> getProductsByCategory(long categoryId) {
		return productRepository.getByField("category.id", categoryId, QueryUtil.EQUAL);
	}

	@Override
	public List<Product> getProductsByCity(long cityId) {
		return productRepository.getByField("store.address.city.id", cityId, QueryUtil.EQUAL);
	}

	@Override
	public List<Product> searchProducts(String keyword, long cityId) {
		return productRepository.searchProducts(keyword, cityId);
	}
}